/**
 * state parameter for User
 *
 * Param : token, visitedViews
 */

import { login, logout, getUserInfo } from '../api/front'

const user = {
  state: {
    user: undefined,
    status: '',
    code: '',
    roles: [],
    menus: undefined, // 用户的可用菜单 menus[code]= true
    elements: undefined, // 用户可用的UI元素(菜单，按钮) elements[code]= true
    permissionMenus: undefined, // 显示给用户看的UI菜单
    authorities: undefined // 用户的授权 ["authority": ] // 用户所有的授权
  },
  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_AUTHORITIES: (state, data) => {
      state.authorities = data
    },
    SET_ELEMENTS: (state, elements) => {
      state.elements = elements
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_PERMISSION_MENUS: (state, permissionMenus) => {
      state.permissionMenus = permissionMenus
    }
  },
  actions: {
    // 用户登录
    USER_LOGIN ({ commit }, userInfo) {
      userInfo.username = userInfo.username.trim()
      commit('SET_MENUS', undefined)
      commit('SET_ELEMENTS', undefined)
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          console.log('store login ok')
          resolve(response)
        }).catch(error => {
          console.log('store login error')
          reject(error)
        })
      })
    },
    // 获取用户信息
    USER_INFO ({ commit }, info) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          if (!response || !response.data) {
            reject(new Error('no user info'))
            return
          }
          let user = response.data
          commit('SET_USER', user)
          commit('SET_ROLES', 'admin')
          // ---- | set authority | ----
          commit('SET_AUTHORITIES', user.authorities)
          let menu = user.authorities
          const menus = {}
          const elements = {}
          for (let i = 0; i < menu.length; i++) {
            let item = menu[i]
            elements[item.authority] = true
            menus[item.authority] = true
          }
          // console.log(menus)
          commit('SET_MENUS', menus)
          // let menu = response.menus
          // const menus = {}
          // for (let i = 0; i < menu.length; i++) {
          //   menus[menu[i].code] = true
          // }
          // // console.log(menus)
          // commit('SET_MENUS', menus)
          // let els = response.authorities
          // const elements = {}
          // for (let i = 0; i < els.length; i++) {
          //   let item = els[i]
          //   if (item.type === '1') {
          //     elements[item.authority] = true
          //   }
          // }
          commit('SET_ELEMENTS', elements)
          resolve(response)
        }).catch(error => {
          // console.log('getUserInfo error')
          reject(error)
        })
      })
    },

    // 用户第三方登录
    // USER_LOGIN_THIRD_PARTY ({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       // setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
    // 用户登出
    USER_LOGOUT ({ commit }) {
      return new Promise((resolve, reject) => {
        commit('SET_MENUS', undefined)
        commit('SET_ELEMENTS', undefined)
        commit('SET_PERMISSION_MENUS', undefined)
        logout().then(() => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    FRONT_LOGOUT ({ commit, state }) {
      return new Promise(resolve => {
        commit('SET_MENUS', undefined)
        commit('SET_ELEMENTS', undefined)
        commit('SET_PERMISSION_MENUS', undefined)
        resolve()
      })
    }
    // CHANGE_ROLE ({ commit, state }, role) { // 动态修改权限
    //   return new Promise(resolve => {
    //     commit('SET_ROLES', [role])
    //     commit('SET_TOKEN', role)
    //     setToken(role)
    //     resolve()
    //   })
    // }
  },
  getters: {
    getUserName (state) {
      let user = state.user
      if (user) {
        return user.username
      }
      return '请登录'
    },
    getUser (state) {
      return state.user
    },
    homeMenus (state) {
      let ms = state.permissionMenus
      return ms
    },
    userMenus (state) { // menu authority {}
      return state.menus
    },
    authorities (state) {
      return state.elements // authority list[auth]=true
    }
  }
}

export default user
